﻿#pragma execution_character_set("utf-8")

#include <QFile>
#include <QJsonValueRef>
#include <QDebug>
#include <QtAlgorithms>

#include "DeviceManager.h"
#include "JsonUtils.h"


DeviceManager::DeviceManager()
{
    _factory=new BaseDeviceFactory();
}

DeviceManager::~DeviceManager()
{
    _isBreak=true;
}

DeviceManager& DeviceManager::Instance()
{
    static DeviceManager _instance;
    return _instance;
}

BaseDevice* DeviceManager::GetDevice(QString name)
{
    return BaseManager::GetItem<BaseDevice>(name);
}

QList<BaseDevice *> DeviceManager::GetDevices(QJsonArray names)
{
    QList<BaseDevice *> ret;
    for(auto item:names)
    {
        ret.append(GetDevice(item.toString()));
    }
    return ret;
}

QList<QString> DeviceManager::GetAllDeviceName()
{
    QList<QString> ret;
    for(auto item:_allDevices)
    {
        ret.append(item->Name());
    }
    return ret;
}

void DeviceManager::SetFactory()
{
    _factory=new BaseDeviceFactory();
}

void DeviceManager::Init()
{
    BaseManager::Init();
}

void DeviceManager::SetWatch(bool isWatch)
{
    _isWatch=isWatch;
}

void DeviceManager::Stop()
{
    for(auto device:GetItems<BaseDevice>())
    {
        device->Stop();
    }
}

void DeviceManager::Reset()
{
    for(auto device:_allDevices)
    {
        device->Reset();
    }
}

void DeviceManager::run()
{
//    while(true)
//    {
//        QThread::msleep(1000);
//        if(_isBreak)
//        {
//            qDebug()<<"状态监控线程停止！";
//            return;
//        }
//        if(_isWatch)
//        {
//            for(auto item:_allDevices)
//            {
//                item->UpdateStatus();
//            }
//        }
//    }
}

void DeviceManager::WaitForEnd()
{
    _isBreak=true;
    //这是监控线程，销毁对程序无不良影响
    qDebug()<<__FUNCTION__<<__LINE__;
//    this->terminate();
    qDebug()<<__FUNCTION__<<__LINE__;
}

